package com.docmgmt.mvp.controller;

import com.docmgmt.mvp.dto.LoginRequest;
import com.docmgmt.mvp.dto.LoginResponse;
import com.docmgmt.mvp.dto.RegisterRequest;
import com.docmgmt.mvp.dto.Result;
import com.docmgmt.mvp.service.AuthService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/api/v1/auth")
@RequiredArgsConstructor
@Tag(name = "1. 身份认证", description = "用户注册、登录等认证接口")
public class AuthController {

    private final AuthService authService;

    @PostMapping("/register")
    @Operation(summary = "用户注册", description = "注册新用户账号，注册成功后自动登录返回JWT令牌")
    public Result<LoginResponse> register(
            @Parameter(description = "注册信息", required = true)
            @Valid @RequestBody RegisterRequest request) {
        log.info("POST /api/v1/auth/register - username={}", request.getUsername());
        LoginResponse response = authService.register(request);
        return Result.success("Registration successful", response);
    }

    @PostMapping("/login")
    @Operation(summary = "用户登录", description = "使用用户名和密码登录，返回JWT令牌。测试账号: admin / password123")
    public Result<LoginResponse> login(
            @Parameter(description = "登录信息", required = true)
            @Valid @RequestBody LoginRequest request) {
        log.info("POST /api/v1/auth/login - username={}", request.getUsername());
        LoginResponse response = authService.login(request);
        return Result.success("Login successful", response);
    }

    @GetMapping("/health")
    @Operation(summary = "健康检查", description = "检查认证服务是否正常运行")
    public Result<Void> health() {
        return Result.success("Auth service is running");
    }
}
